<template>
  <div class="message-container">
    <div v-if="props.message.isUser" class="user_message" :class="{'image-top':props.message.type!='font'}">
      <div class="avator">
        <img src="../assets/images/灵医.png" width="60px" height="60px">
      </div>
      <span v-if="props.message.type=='font'" style="letter-spacing:1px;margin-right: 5px;">{{ props.message.value }}</span>
      <div v-else class="picture">
        <img :src="props.message.value" width="200px" height="200px"/>
      </div>
    </div>
    <div v-else class="ai_message">
      <div class="avator">
        <img src="../assets/images/user.png" width="50px" height="50px">
      </div>
      <span v-if="props.message.type==0" style="letter-spacing:1px;margin-left: 5px;">{{ props.message.content}}</span>
      <div v-else-if="props.message.type==1" class="doc_picture">
        <img :src="props.message.content" width="200px" height="200px"/>
      </div>
      <span v-else style="letter-spacing:1px;margin-left: 5px;">{{ props.message }}</span>
    </div>
  </div>
</template>

<script setup>
import { reactive,onMounted } from 'vue';

const message=reactive({})

onMounted(()=>{
  if(isJSON(message)){
    message=JSON.parse(message)
  }
})

const props=defineProps({
  message:{
    type:Object,
  }
})

const isJSON=(string)=>{
  try{
    JSON.parse(string)
    return true
  }catch(e){
    return false
  }
}

</script>

<style lang="scss" scoped>
.message-container {
  display: flex;
  flex-direction: column;
  margin-top: 20px;
  padding-right: 30px;
}

.user_message,
.ai_message {
  display: flex;
  // align-items: center;
  margin-bottom: 20px; 
  .doc_picture{
    display: block;
    margin-top: 10px;
    margin-left: 5px;
  }
  .avator {
    background-color: #fff;
    width: 65px;
    height: 65px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  span {
    font-size: 20px;
    margin-left: 10px;
    background-color: #fff;
    min-width: 20px;
    max-width: 600px;
    min-height: 35px;
    padding: 15px 10px;
    border-radius: 12px;
  }
}

.picture {
  display: block;
  margin-top: 10px;
  margin-right: 5px;
}

.image-top {
  margin-top: 20px;
}

.ai_message {
  margin-left: 20px;
  .avator {
    margin-left: 10px;
  }
}

.user_message {
  flex-direction: row-reverse;
  .avator {
    margin-right: 10px;
  }
}
</style>
